﻿using FileApi.Entity;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using FileApi.common;
using static CoreApi.common.Constant;
using static Dm.net.buffer.ByteArrayBuffer;
using Microsoft.AspNetCore.Authorization;
using System.Net;

namespace FileApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class CommonController : ControllerBase
    {
        private readonly ISqlSugarClient _dbContext;
        public CommonController(ISqlSugarClient dbContext)
        {
            _dbContext = dbContext;
        }
        /// <summary>
        /// 获取总数
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ResponseView GetCount()
        {
            ResponseView r=new ResponseView();
            try
            {
                int count = _dbContext.Queryable<base_user>().Count();
                r.data = count;
            }
            catch (Exception ex)
            {
                r.code = 400;
                r.msg = "失败" + ex.ToString();
            }
            return r;
        }

        /// <summary>
        /// 获取用户详情信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public OutMsg<List<base_user>> GetUserDetail(string username)
        {
            OutMsg<List<base_user>> r = new OutMsg<List<base_user>>();
            try
            {
                List<base_user> list = _dbContext.Queryable<base_user>()
                    .WhereIF(!string.IsNullOrEmpty(username), i => i.username == username)
                    .ToList();
                r.data = list;
                r.code = ResponseCode.Success;
            }
            catch (Exception ex)
            {
                r.code = ResponseCode.Error;
                r.msg = "获取用户信息时出现异常:" + ex.Message; 
            }
          return r;
        }

        /// <summary>
        /// 用户列表带分页
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public OutMsg<TableListResponseModel<List<base_user>>> UserInfoList(TableListSearchModel<base_userModel> model)
        {
            OutMsg<TableListResponseModel<List<base_user>>> OutMsg = new OutMsg<TableListResponseModel<List<base_user>>>();
            TableListResponseModel<List<base_user>> data = new TableListResponseModel<List<base_user>>();
            if (String.IsNullOrEmpty(model.order))
            {
                model.order = "id desc";
            }
            int total = 0;
            try
            {
                var list = _dbContext.Queryable<base_user>()
               .WhereIF(!string.IsNullOrEmpty(model.where.username), it => it.username == model.where.username)
               .WhereIF(!string.IsNullOrEmpty(model.where.address), it => it.address == model.where.address)
               .OrderBy(model.order)
               .ToPageList(model.pageIndex, model.pageSize, ref total)
               .ToList();
                data.list = list;
                data.total = total;
                OutMsg.data = data;
            }
            catch (Exception ex)
            {

                OutMsg.code = ResponseCode.Error;
                OutMsg.msg = "获取用户信息时出现异常:" + ex.Message;
            }
           

            return OutMsg;
        }



        /// <summary>
        /// 更新用户信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResponseView UpdateUserInfo(base_user model)
        {
            ResponseView r = new ResponseView();
            base_user u =new base_user();
            try
            {
                u.username = model.username;
                u.email = model.email;
                u.address = model.address;
                u.phone = model.phone;
                u.password= model.password;
                u.age= model.age;
                u.ah=model.ah;
                u.pic=model.pic;
                u.major=model.major;
                u.sex=model.sex;
                u.roleText=model.roleText;
                var result = _dbContext.Updateable(model).Where(i=>i.username==model.username).ExecuteCommand();
                if (result<=0)
                {
                    r.msg = "更新失败";
                    r.code = 400;
                }
            }
            catch (Exception ex)
            {
                r.code = 400;
                r.msg = "更新失败:" + ex.Message;
            }
            return r;
        }



    }
}
